#include "LogClient.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <QCoreApplication>

int main(int argc, char *argv[]) {
    // Create Qt application instance
    QCoreApplication app(argc, argv);
    
    // Get LogClient instance
    std::cout << "LogClient instance created" << std::endl;

    LogClient& logClient = LogClient::getInstance();
    
    // Set connection callback
    logClient.setConnectionCallback([](bool connected) {
        std::cout << "Connection status: " << (connected ? "Connected" : "Disconnected") << std::endl;
    });
    
    // Set device ID and session ID
    logClient.setDeviceId("CppTestDevice001");
    logClient.setSessionId("Session_" + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count()));
    
    std::cout << "Device ID: " << logClient.getDeviceId() << std::endl;
    std::cout << "Session ID: " << logClient.getSessionId() << std::endl;
    std::cout << "Testing device registration..." << std::endl;
    
    // Initialize WebSocket connection
    std::string serverUrl = "ws://localhost:3333";
    if (!logClient.initialize(serverUrl)) {
        std::cerr << "Failed to initialize LogClient" << std::endl;
        return 1;
    }
    
    std::cout << "LogClient initialized, connecting to " << serverUrl << std::endl;
    
    // Wait for connection
    std::this_thread::sleep_for(std::chrono::seconds(2));
    
    std::cout << "Sending test logs..." << std::endl;
    // Send different level logs
    logClient.info("System", "Application started");
    logClient.info("Database", "Database connection successful");
    logClient.warning("Network", "High network latency detected");
    logClient.error("Database", "Query execution failed");
    logClient.exception("System", "Uncaught exception occurred");
    logClient.emergency("Security", "Unauthorized access attempt detected");
    
    // Use macros to send logs
    LOG_INFO("UI", "User logged in");
    LOG_WARNING("System", "High memory usage detected");
    LOG_ERROR("FileSystem", "Cannot write to file");
    
    // Send some repeated logs for testing
    for (int i = 0; i < 3; i++) {
        LOG_WARNING("System", "Disk space low");
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    
    // sleep(10);
    std::cout << "Test logs sent. Press Enter to exit..." << std::endl;
    std::cin.get();
    
    // Cleanup
    logClient.shutdown();
    
    return 0;
}